package com.sy.controller.admin;

import com.sy.constant.RedisConstant;
import com.sy.dto.CoursedetailPageQueryDTO;
import com.sy.pojo.Coursedetail;
import com.sy.result.PageResult;
import com.sy.result.Result;
import com.sy.service.CoursedetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 课程明细管理
 */
@Api(tags = "课程明细相关接口")
@RestController
@RequestMapping("/admin/courseDetail")
@Slf4j
public class CourseDetailController {

    @Autowired
    private CoursedetailService coursedetailService;

    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * 课程明细分页查询
     * @param coursedetailPageQueryDTO
     * @return
     */
    @ApiOperation("课程明细分页查询")
    @GetMapping("/findAll")
    public Result<PageResult> findAll(CoursedetailPageQueryDTO coursedetailPageQueryDTO) {
        //打印请求数据日志
        log.info("课程分页查询：{}", coursedetailPageQueryDTO);
        //调用业务层实现分页查询
        PageResult pageResult = coursedetailService.findAll(coursedetailPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 添加课程明细
     *
     * @param coursedetail
     * @return
     */
    @ApiOperation("添加课程明细")
    @PostMapping("/save")
    public Result<String> save(@RequestBody Coursedetail coursedetail) {
        try {
            //设置添加课程的当前时间
            coursedetail.setCreateTime(LocalDateTime.now());
            coursedetailService.save(coursedetail);
            //将课程明细的文件信息保存到redis中（有效文件）
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_DB,coursedetail.getUrl());
            return Result.success();
        } catch (Exception e) {
            //e.printStackTrace();
            throw new RuntimeException("课程添加失败");
        }
    }
}